﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mPPC.Common
{
    public class CanelHelper
    {
        #region  待转PTX取消
        /// <summary>
        /// 删除【PPC_HPL430_Cutting_State】
        /// </summary>
        /// <returns></returns>
        public static bool Delete_Cutting_State_Cancel(string Optimization_No)
        {
            string sql_update = @"delete from [LamexMESManager].[dbo].[PPC_HPL430_Cutting_State] 
            where Optimization_No='" + Optimization_No + "'";
            int n = Lamex.Base.BaseBuildSql.ExecuteNonQuery(sql_update);
            if (n > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 删除【PPC_HPL430_CSV】
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public static bool Delete_CSV_OPT(string Optimization_No)
        {
            string sql_update = @"delete from [LamexMESManager].[dbo].[PPC_HPL430_CSV] 
            where INFO_20='" + Optimization_No + "'";
            int n = Lamex.Base.BaseBuildSql.ExecuteNonQuery(sql_update);
            if (n > 0)
                return true;
            else
                return false;
        }
        #endregion
        #region 待转PTX明细-按流水号删除
        /// <summary>
        /// 删除待转PTX-csv记录【流水号删除】
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public static bool Update_CSV(List<string> barcode)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(" AND BARCODE IN ('{0}')", string.Join("','", barcode.ToArray()));
            string sql_update = @"update [LamexMESManager].[dbo].[PPC_HPL430_CSV] 
            set DELETE_FLAG='1' where 1=1  " + stringBuilder.ToString() + "";
            int n = Lamex.Base.BaseBuildSql.ExecuteNonQuery(sql_update);
            if (n > 0)
                return true;
            else
                return false;
        }
        #endregion
        #region 取消PTX文件-包含HPO已读入
        /// <summary>
        /// 取消PTX文件-包含HPO已读入
        /// </summary>
        /// <param name="root_CSV">toConvertCSV路径</param>
        /// <param name="keyValues">订单号和状态字典表</param>
        public static void Delete_PTX_HPO(DirectoryInfo root_CSV, Dictionary<string, string> keyValues)
        {
            foreach (var item in keyValues.Keys)
            {
                //删除表【PPC_HPL430_Cutting_State】的记录
                Common.CanelHelper.Delete_Cutting_State_Cancel(item);
                //删除表【PPC_HPL430_CSV】的记录
                Common.CanelHelper.Delete_CSV_OPT(item);
                //移除已生成的csv文件
                foreach (FileInfo f in root_CSV.GetFiles())
                {
                    if (f.Name.Contains(item))
                    {
                        if (File.Exists(f.FullName))
                            File.Delete(f.FullName);
                    }
                }
                //移除HPO已读入的csv文件
                foreach (DirectoryInfo NextFolder in root_CSV.GetDirectories())
                {
                    //获取子文件夹所有csv文件
                    foreach (FileInfo f in NextFolder.GetFiles())
                    {
                        if (f.Name.Contains(item))
                        {
                            if (File.Exists(f.FullName))
                                File.Delete(f.FullName);
                            continue;
                        }
                    }
                }
            }
        }
        #endregion
        #region 取消PTX文件-正常生成的HPO
        /// <summary>
        /// 取消PTX文件-正常生成的HPO
        /// </summary>
        /// <param name="root_CSV">toConvertCSV路径</param>
        /// <param name="keyValues">订单号和状态字典表</param>
        public static void Delete_PTX_NOHPO(DirectoryInfo root_CSV, Dictionary<string, string> keyValues)
        {
            foreach (var item in keyValues.Keys)
            {
                //删除表【PPC_HPL430_Cutting_State】的记录
                Common.CanelHelper.Delete_Cutting_State_Cancel(item);
                //删除表【PPC_HPL430_CSV】的记录
                Common.CanelHelper.Delete_CSV_OPT(item);
                //移除已生成的csv文件
                foreach (FileInfo f in root_CSV.GetFiles())
                {
                    if (f.Name.Contains(item))
                    {
                        if (File.Exists(f.FullName))
                            File.Delete(f.FullName);
                    }
                }        
            }
        }
        #endregion
    }
}
